// pages/mine/find/vote/detail/detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [
      'http://m.qpic.cn/psb?/V10uH3z53mPeWp/zQsZCYof18C1Kl1zG*dUsUMgcVz44gTNspqqwJHH5KI!/b/dFEBAAAAAAAA&bo=ZwlABkcQ0goRCZk!&rf=viewer_4'
    ,],
    disable: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    is_show: false,
    details: {},
  },
  toHome(){
    console.log('跳转')
    wx.reLaunch({
      url: '../vote'
    })
  },
  getVote(){
    var that=this
    wx.request({
      url: app.globalData.url + 'college/detail', // 仅为示例，并非真实的接口地址
      data: {
        user_id:app.globalData.user_id,
        college_id:that.data.details.id
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if(res.data.state==1)
        {
          that.setData({
            details: res.data.data,
            vote_num:res.data.vote_num
          })
        }

      }
    })

  },
  toHold(){
    var that = this
    that.setData({
      is_load: false
    })
    wx.request({
      url: app.globalData.url + 'college', // 仅为示例，并非真实的接口地址
      data: {
        user_id: app.globalData.user_id,
        college_id: that.data.details.id
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log('----投票结果----')
        console.log(res.data)
        if (res.data.state == 1) {
          wx.showToast({
            title: '感谢支持^v^',
            icon: 'success',
            duration: 2000
          })
          var d =that.data.details
          d.is_vote = true
          d.num +=1
          that.setData({
            is_load: true,
            details: d,
            vote_num: that.data.vote_num + 1
          })
          that.onLoad()

        }
      }
    })
      
  },
  bindGetUserInfo(e) {
    var that = this;
    that.onLoad()
    if (e.detail.userInfo) {
      console.log(e.detail.userInfo.nickName)
      app.globalData.userInfo = e.detail.userInfo
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: app.globalData.url + 'user/code',
            data: {
              code: res.code,
              nick: e.detail.userInfo.nickName,
              avatar: e.detail.userInfo.avatarUrl,
            },
            method: "POST",
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              app.globalData.openid = res.data.data.openid //返回openid
              app.globalData.user_id = res.data.user_id
              console.log("------user_id------")
              console.log(res.data.user_id)
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let item = JSON.parse(options.jsonStr);
    console.log(item)
    this.setData({
      details: item,
      swiperList: [item.img]
    });

    var that = this;
    //that.down()
    that.setData({
      is_load: false,

    })
    wx.login({
      success(res) {
        if (res.code) {
          // 发起网络请求
          wx.request({
            url: app.globalData.url + 'user/code/' + res.code,
            data: {},
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              console.log('----user-----')
              console.log(res.data.user_id)
              app.globalData.user_id = res.data.user_id
              that.getVote()
            },
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    wx.getSetting({

      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log(res.userInfo)
              that.setData({
                is_load: true
              })
            }
          })
          that.setData({
            is_show: false
          })
        } else {
          that.setData({
            is_load: true,
            is_show: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that =this
    let str = JSON.stringify(that.data.details) ;
    return {
      title: '还差一票就赢了，快来帮我一下',
      path: 'pages/mine/find/vote/detail/detail?jsonStr=' + str,
      imageUrl: that.data.swiperList[0], //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。

    }
  }
})